package cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.addAfterSign.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.domain.task.type.AddSignTaskRequest;
import cn.schoolwow.workflow.domain.task.type.AddSignType;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import org.junit.Assert;

import java.util.Arrays;

public class AddAfterSignCompleteAnalyzeWorkReportFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setWorkFlowTaskQuery(flowContext);
        addAfterSign(flowContext);
        handleAddAfterSignerTask(flowContext);
        completeTask(flowContext);
    }

    @Override
    public String name() {
        return "后加签任务且被加签人通过分析工作报告任务";
    }

    private void setWorkFlowTaskQuery(FlowContext flowContext){
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
        workFlowTaskQuery.instanceStatusList.add(WorkFlowInstanceStatus.Running.status);
        workFlowTaskQuery.complete = false;
        flowContext.putCurrentFlowInstanceData(workFlowTaskQuery);
    }

    private void addAfterSign(FlowContext flowContext){
        WorkFlowTaskQuery workFlowTaskQuery = flowContext.checkInstanceData(WorkFlowTaskQuery.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        String firstAuditUserId = startWorkFlowRequest.contextData.getString("firstAuditUserId");
        workFlowTaskQuery.assignerList = Arrays.asList(firstAuditUserId);

        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);

        AddSignTaskRequest addSignTaskRequest = new AddSignTaskRequest();
        addSignTaskRequest.addSignType = AddSignType.ADD_AFTER_SIGN;
        addSignTaskRequest.taskId = workFlowTaskResponse.taskId;
        addSignTaskRequest.signAssignerList = Arrays.asList("王五", "赵六");
        flowContext.putCurrentFlowInstanceData(addSignTaskRequest);
        quickWorkFlow.addSignWorkFlowTask(addSignTaskRequest);

        flowContext.putCurrentFlowInstanceData(addSignTaskRequest);
    }

    private void handleAddAfterSignerTask(FlowContext flowContext){
        AddSignTaskRequest addSignTaskRequest = flowContext.checkInstanceData(AddSignTaskRequest.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        for(String assigner:addSignTaskRequest.signAssignerList){
            workFlowTaskQuery.assignerList = Arrays.asList(assigner);
            WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
            quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
        }
    }

    private void completeTask(FlowContext flowContext){
        WorkFlowTaskQuery workFlowTaskQuery = flowContext.checkInstanceData(WorkFlowTaskQuery.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        String secondAuditUserId = startWorkFlowRequest.contextData.getString("secondAuditUserId");

        //二级审批人同意
        workFlowTaskQuery.assignerList = Arrays.asList(secondAuditUserId);
        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
        boolean instanceFinished = quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
        Assert.assertTrue("实例当前已经结束", instanceFinished);
    }
}
